<?php

/**
 * Description of Dao
 *
 * @author ghanemel
 */
class Dao extends CI_Model {

    public function __construct() {
        parent::__construct();
        $this->load->database();
    }

    public function insertDiscussion($data) {
        return $this->db->insert('discussion', $data);
    }

    public function insertTodo($data) {
        return $this->db->insert('todo', $data);
    }

    public function insertNotification($data) {
        return $this->db->insert('notification', $data);
    }

    public function UserNotifsPlus($data) {
        $sql = "update user_notifications set notifications=notifications+1 where user='" . $data['user'] . "'";
        $this->db->query($sql);
        $update = $this->db->affected_rows();
        if ($update > 0) {
            return $update;
        } else {
            $this->db->insert('user_notifications', $data);
            return $this->db->affected_rows();
        }
    }

    public function UserNotifsMoins($data) {
        $notifToDelete = $data['notifToDelete'];
        $sql = "delete from notification where id='" . $notifToDelete . "'";
        $this->db->query($sql);
        $sql = "update user_notifications set notifications=notifications-1 where notifications > 0 and user='" . $data['user'] . "'";
        $this->db->query($sql);
        return $this->db->affected_rows();
    }

    public function verifOnDb($login, $pass) {
        $sql = "select * from user where login='$login' and password='$pass'";
        $query = $this->db->query($sql);
        $this->db->select('*');
        $this->db->from('user');
        $this->db->where('login', $login);
        $query = $this->db->get();
        if ($query->num_rows() > 0) {
            $result = $query->result();
            foreach ($result as $row) {
                $this->session->set_userdata('logged_in_user', $row);
            }
            return TRUE;
        } else {
            return FALSE;
        }
    }

    public function findDiscussion($userId, $to) {
        $this->db->select('*');
        $this->db->from('discussion');
        $this->db->where('user', $userId);
        $this->db->where('to_user', $to);
        $this->db->or_where('to_user', $userId);
        $this->db->where('user', $to);
        $query = $this->db->get();
        $result = $query->result();
        foreach ($result as $row) {
            $user = $this->findUserById($row->user);
            $to_user = $this->findUserById($row->to_user);
            $row->user = $user;
            $row->to_user = $to_user;
        }
        return $result;
    }

    public function findUserById($id) {
        $this->db->select('*');
        $this->db->from('user');
        $this->db->where('id', $id);
        $query = $this->db->get();
        $user = $query->result();

        if (isset($user[0])) {
            $user[0]->password = "********";
            return $user[0];
        } else {
            return null;
        }
    }

    public function findNotifsNbr($Userid) {
        $this->db->select('*');
        $this->db->from('user_notifications');
        $this->db->where('user', $Userid);
        $query = $this->db->get();
        $notifUsr = $query->result();
        if (sizeof($notifUsr) > 0) {
            $u = $notifUsr[0];
            return $u->notifications;
        } else {
            return 0;
        }
    }

    public function findNotifsNbrByUser($userId) {
        // $sql = "select from_user, count(id) from notification where user='$userId' group by from_user";
        $this->db->select('from_user, count(id) as nbr_ntfs');
        $this->db->from('notification');
        $this->db->where('user', $userId);
        $this->db->group_by('from_user');
        $query = $this->db->get();
        return $query->result();
    }

    /* returns json object */

    public function findNotifs($userId) {
        $this->db->select('*');
        $this->db->from('notification');
        $this->db->where('user', $userId);
        $query = $this->db->get();
        $result = $query->result();
        //print_r($result);
        if (sizeOf($result) > 0) {
            foreach ($result as $row) {
                $action_id = $row->action;
                $action = $this->findActionById($action_id);
                switch ($action_id) {
                    case 1 :
                        $row->object = $this->findDiscussionById($row->object_id);
//                        if ($row->object->user->id == $withId) {
//                            $action->url = "process";
//                            $dataUserNotifs = array(
//                                'id' => 'null',
//                                'user' => $userId,
//                                'notifications' => '1',
//                                'notifToDelete' => $row->id
//                            );
//                            $this->UserNotifsMoins($dataUserNotifs);
//                        }
                        break;
                }
                $row->action = $action;
            }
        }
        return $result;
    }

    public function findActionById($id) {
        $this->db->select('*');
        $this->db->from('action');
        $this->db->where('id', $id);
        $query = $this->db->get();
        $result = $query->result();
        if (sizeof($result) > 0) {
            return $result[0];
        } else {
            return null;
        }
    }

    public function findDiscussionById($id) {
        $this->db->select('*');
        $this->db->from('discussion');
        $this->db->where('id', $id);
        $query = $this->db->get();
        $result = $query->result();
        if (sizeOf($result) > 0) {
            $user = $this->findUserById($result[0]->user);
            $to_user = $this->findUserById($result[0]->to_user);
            $result[0]->user = $user;
            $result[0]->to_user = $to_user;
            return $result[0];
        } else {
            return null;
        }
    }

    public function findUsers($userId) {
        $this->db->select('*');
        $this->db->from('user');
        $query = $this->db->get();
        $result = $query->result();
        if (sizeOf($result) > 0) {
            foreach ($result as $v) {
                $this->db->select('count(id) as countId ');
                $this->db->from('notification');
                $this->db->where('user', $userId);
                $this->db->where('from_user', $v->id);
                $query = $this->db->get();
                $res = $query->result();
                $countId = $res[0]->countId;
                $v->notifsForMe = $countId;
                //Get city and country :
                $v->city = $this->findCityById($v->city);
                $v->country = $this->getCountryById($v->country);
            }
            return $result;
        } else {
            return array();
        }
    }

    public function findCityById($id) {
        $this->db->select('*');
        $this->db->from('city');
        $this->db->where('id', $id);
        $queryCity = $this->db->get();
        $resultCity = $queryCity->result();

        if (isset($resultCity[0])) {
            return $resultCity[0];
        } else {
            return null;
        }
    }

    public function getCountryById($id) {
        $this->db->select('*');
        $this->db->from('country');
        $this->db->where('id', $id);

        $queryCountry = $this->db->get();
        $queryCountry = $queryCountry->result();

        if (isset($queryCountry[0])) {
            return $queryCountry[0];
        } else {
            return null;
        }
    }

    public function deleteAllNotificationsForDiscussion($userId, $discId) {
        $sql = "delete from notification where object='discussion' and object_id='$discId'";
        $this->db->query($sql);
        $update = $this->db->affected_rows();
        //echo $update . "<br/>";
        $sql = "update user_notifications set notifications=notifications-$update where user='" . $userId . "'";
        $this->db->query($sql);
        $update = $this->db->affected_rows();
    }

    public function delNotifById($ntfId) {
        $sql = "delete from notification where id='$ntfId'";
        $this->db->query($sql);
        echo $this->db->affected_rows();
    }

    public function delNotifs($notifs, $userId) {
        $sql = "delete from notification where id in ($notifs)";
        $this->db->query($sql);
        $nbrAff = $this->db->affected_rows();
        $sql = "update user_notifications set notifications=notifications-$nbrAff where notifications > 0 and user='" . $userId . "'";
        $this->db->query($sql);
        return $nbrAff;
    }

    public function findTodos() {
        $this->db->select('*');
        $this->db->from('todo');
        $this->db->order_by('dt_done, dt_insertion asc');
        return $this->db->get()->result();
    }

    public function updateToDone($id) {
        $sql = "update todo set dt_done='" . date('Y-m-d H:i:s', now()) . "' where id='" . $id . "'";
        $this->db->query($sql);
        $update = $this->db->affected_rows();
    }

    public function searchForUsers($keywords) {
        $userId = $this->session->userdata('logged_in_user')->id;
        $this->db->select('*');
        $this->db->from('user');
        $this->db->where('match(firstname,lastname) against(\'' . $keywords . '\' IN BOOLEAN MODE)');
        $result = $this->db->get()->result();
        if (sizeOf($result) > 0) {
            foreach ($result as $v) {
                $this->db->select('count(id) as countId ');
                $this->db->from('notification');
                $this->db->where('user', $userId);
                $this->db->where('from_user', $v->id);
                $query = $this->db->get();
                $res = $query->result();
                $countId = $res[0]->countId;
                $v->notifsForMe = $countId;
                //Get city and country :
                $v->city = $this->findCityById($v->city);
                $v->country = $this->getCountryById($v->country);
            }
            return $result;
        } else {
            return array();
        }
    }

}

?>